<!--广告管理-->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="广告名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入广告名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="位置" prop="position">
        <dict-select :options="dict.type.position_options" v-model="queryParams.position" placeholder="请选择位置"/>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <dict-select :options="dict.type.advertision_status" v-model="queryParams.state" placeholder="请选择位置"/>
      </el-form-item>
      <el-form-item label="发布时间" prop="state">
        <date-range-picker v-model="queryParams" value-format="timestamp" content-style="width: 240px"
                           startPickerName="startTime" endPickerName="endTime"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain size="mini" @click="$router.push({path:'/content/advertisionAdd'})" v-hasPermi="['video:list:add']">
            添加
        </el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="advertisementList">
      <el-table-column label="广告名称" align="center" prop="name" width="140" :show-overflow-tooltip="true"/>
      <el-table-column label="位置" align="center" prop="position" width="140" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.position_options" :value="scope.row.position"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state" width="100">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.state" active-color="#13ce66" inactive-color="#ff4949" :active-value="0"
                     :inactive-value="1" @change="statusChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="发布人" align="center" prop="createBy" width="140" :show-overflow-tooltip="true"/>
      <el-table-column label="发布时间" align="center" prop="createTime" width="140" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ parseTime(scope.row.createTime, '{m}/{d}/{y}') }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <div>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="$router.push({path:'/content/advertisionAdd',query:{id:scope.row.id}})"
              v-hasPermi="['car:advertisement:edit']"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="showDetails(scope.row)"
              v-hasPermi="['car:advertisement:edit']"
            >查看
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['car:advertisement:remove']"
            >删除
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详情 -->
    <el-dialog title="广告详情" :visible.sync="open" width="800px" append-to-body>
      <el-card>
        <el-row class="details-content-row">
          <el-col :span="3">广告名称</el-col>
          <el-col :span="16">{{ currentObj.name }}</el-col>
        </el-row>
        <el-row class="details-content-row">
          <el-col :span="3">展示位置</el-col>
          <el-col :span="16">
            <dict-tag :options="dict.type.position_options" :value="currentObj.position"></dict-tag>
          </el-col>
        </el-row>
        <el-row class="details-content-row">
          <el-col :span="3">状态</el-col>
          <el-col :span="16">
            <dict-tag :options="dict.type.advertision_status" :value="currentObj.state"></dict-tag>
          </el-col>
        </el-row>
        <el-row class="details-content-row">
          <el-col :span="3">创建时间</el-col>
          <el-col :span="16">{{ parseTime(currentObj.createTime, '{m}/{d}/{y}') }}</el-col>
        </el-row>
        <el-row class="details-content-row">
          <el-col :span="3">广告内容</el-col>
        </el-row>
        <el-table :data="currentObj.platformAdvertisementDetailVOS">
          <el-table-column label="排序" align="center" prop="sortNum" width="140" :show-overflow-tooltip="true"/>
          <el-table-column label="图片" align="center" prop="imgUrl" width="160">
            <template slot-scope="scope">
              <oss-image-preview :src="scope.row.imgUrl" :width="40"/>
            </template>
          </el-table-column>
          <el-table-column label="跳转链接" align="center" prop="url" width="220" :show-overflow-tooltip="true"/>
        </el-table>
      </el-card>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAdvertisement,
  delAdvertisement,
  getAdvertisement,
  updateState
} from '@/api/content/advertisionList'
import Template from '@/views/setting/template/index.vue'
import DateRangePicker from '@/components/DateRangePicker/index.vue'
import OssImagePreview from '@/components/ImagePreview/oss-image-preview.vue'

export default {
  name: 'Advertisement',
  components: { OssImagePreview, DateRangePicker, Template },
  dicts: ['position_options', 'advertision_status'],
  data() {
    return {
      positionOptions: [{ value: 0, label: '平台公告' }, { value: 1, label: '推送消息' }],
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 平台广告管理表格数据
      advertisementList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        position: null,
        state: null,
        deleted: null
      },
      currentObj: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询平台广告管理列表 */
    getList() {
      this.loading = true
      listAdvertisement(this.queryParams).then(response => {
        this.advertisementList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryParams = {
        pageNum: 1,
          pageSize: 10,
          name: null,
          position: null,
          state: null,
          deleted: null
      }
      this.handleQuery()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除平台广告管理编号为"' + ids + '"的数据项？').then(function() {
        return delAdvertisement(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 修改状态 */
    statusChange(row) {
      updateState({ state: row.state, id: row.id }).then(response => {
        this.$modal.msgSuccess('修改成功')
        this.getList()
      })
    },
    /** 修改状态 */
    showDetails(row) {
      getAdvertisement(row.id).then(response => {
        this.currentObj = response.data
        this.open = true
      })
    }
  }
}
</script>
<style scope lang="scss">
.details-content-row {
  padding: 10px 0;
}
</style>
